package com.chen.shopping_cart_service.service;

import com.chen.shopping_common.pojo.CartGoods;
import com.chen.shopping_common.service.CartService;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Map;

@DubboService
public class CartServiceImpl implements CartService {

    @Autowired
    private RedisTemplate redisTemplate;

    @Override
    public void addCart(Long userId, CartGoods cartGoods) {
        //从redis中获取用户的购物车列表
        List<CartGoods> cartList = getCartList(userId);
        //如果该商品已在购物车中，添加商品数量
        for (CartGoods cartGoods1 : cartList) {
            if (cartGoods.getGoodId().equals(cartGoods1.getGoodId())){
                int num = cartGoods.getNum()+cartGoods1.getNum();
                cartGoods1.setNum(num);
                redisTemplate.boundHashOps("cartList").put(userId,cartList);
                return;
            }
        }
        //如果商品还没有在购物车中，添加商品到购物车
        cartList.add(cartGoods);
        redisTemplate.boundHashOps("cartList").put(userId,cartList);
    }

    @Override
    public void updateCart(Long userId, Long goodsId, Integer number) {
        //获取购物车列表
        List<CartGoods> cartList = getCartList(userId);
        //找到对应商品
        for (CartGoods cartGoods : cartList) {
            if (cartGoods.getGoodId().equals(goodsId)){
                //改变商品数量
                cartGoods.setNum(number);
                break;
            }
        }
        //存入redis中
        redisTemplate.boundHashOps("cartList").put(userId,cartList);
    }

    @Override
    public void deleteCart(Long userId, Long goodId) {
        //获取购物车列表
        List<CartGoods> cartList = getCartList(userId);
        //移除商品
        for (CartGoods cartGoods : cartList) {
            if (goodId.equals(cartGoods.getGoodId())){
                cartList.remove(cartGoods);
                break;
            }
        }
        //存入redis中
        redisTemplate.boundHashOps("cartList").put(userId,cartList);
    }

    @Override
    public List<CartGoods> getCartList(Long userId) {
        //从redis中获取购物车集合
        Object cart = redisTemplate.boundHashOps("cartList").get(userId);
        //返回结果
        if (cart == null ){
            return new ArrayList<CartGoods>();
        }else {
            return (List<CartGoods>)cart;
        }
    }

    //更新所有用户的商品数据
    @Override
    public void updateGoods(CartGoods cartGoods) {
        //获取所有用户的购物车
        Map<Long,List<CartGoods>> cartList1 = redisTemplate.boundHashOps("cartList").entries();
        Collection<List<CartGoods>> cartList = cartList1.values();
        //遍历所有购物车
        for (List<CartGoods> goods : cartList) {
            for (CartGoods good : goods) {
                if (good.getGoodId().equals(cartGoods.getGoodId())){
                    good.setGoodsName(cartGoods.getGoodsName());
                    good.setHeaderPic(cartGoods.getHeaderPic());
                    good.setPrice(cartGoods.getPrice());
                }
            }
        }

        //存入redis中
        redisTemplate.delete("cartList");
        redisTemplate.boundHashOps("cartList").putAll(cartList1);
    }

    //从所有用户的购物车中删除这个商品
    @Override
    public void deleteGoods(Long goodsId) {
        //获取所有用户的购物车
        Map<Long,List<CartGoods>> cartList1 = redisTemplate.boundHashOps("cartList").entries();
        Collection<List<CartGoods>> cartList = cartList1.values();
        //遍历所有购物车
        for (List<CartGoods> goods : cartList) {
            for (CartGoods good : goods) {
                if (goodsId.equals(good.getGoodId())){
                    goods.remove(good);
                    break;
                }
            }
        }

        //存入redis中
        redisTemplate.delete("cartList");
        redisTemplate.boundHashOps("cartList").putAll(cartList1);
    }
}
